﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using System.Configuration;
using System.Net.Mail;

namespace WebApp
{
    public partial class ForgetPwd : System.Web.UI.Page
    {
        private string mailServer = ConfigurationManager.AppSettings["SmtpServer"];
        private string mailUserName = ConfigurationManager.AppSettings["SmtpUserName"];
        private string mailPassword = ConfigurationManager.AppSettings["SmtpPassWord"];

        private UserManager m_UserManager = new UserManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string UserName = this.TextBox1.Text.Trim();
            string Email = this.TextBox2.Text.Trim();


            Cosmetic.DataEntity.User auser = m_UserManager.GetUserByName(UserName);
            if (auser.Email!=Email)
            {
                Cosmetic.WebGeneric.WebMessageBox("您填写的信息与账户信息不符!", this.Page);
                return;
            }
            SmtpClient theSmtp = new SmtpClient();
            theSmtp.Host = mailServer;
            theSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            theSmtp.Port = 25;
            theSmtp.UseDefaultCredentials = false;
            theSmtp.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassword);
            MailMessage theMail = new MailMessage();
            //获取自己的邮箱地址
            theMail.From = new MailAddress(mailUserName);
            theMail.To.Add(new MailAddress(Email));
            theMail.Subject = "来自日花商城的信函：会员密码找回。";
            theMail.Priority = MailPriority.High;
            theMail.BodyEncoding = System.Text.Encoding.UTF8;
            theMail.IsBodyHtml = true;
            string body = "亲爱的 " + auser.UserName + "' <br/> 您的密码是：" + auser.Password + " <br/>请妥善保管! ";
            theMail.Body = body;
            //theMail.Body = stringBuilder.ToString();
            try
            {
                theSmtp.Send(theMail);
                Cosmetic.WebGeneric.WebMessageBox("您的密码已经发送到您的邮箱!", this.Page);
            }
            catch (Exception ex)
            {
            }
        }
    }
}